package com.xzz.sort;

import java.util.Arrays;

/**
 * @author: hhz
 * @create: 2021-12-29 10:56
 * 选择排序
 **/
public class SelectSort {
    public static void main(String[] args) {
        int[] arr = new int[]{4, 6, 8, 5,7, 9, 2, 3,7};
        selectSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length ; i++) {
            int min = i;
            for (int j = i + 1; j < arr.length ; j++) {
                min = arr[j] > arr[min] ? min : j;
            }
            if(min!=i){
                swap(arr, i, min);
            }

        }
    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
